# Maintainer: Dave Murphy <davem@devkitpro.org>
# Contributor: Ezekiel Bethel <stary@9net.org>
# Contributor: carstene1ns <dev f4ke de>
# Contributor: jakibaki <jakibaki live com>

pkgname=switch-ffmpeg
pkgver=4.4.4
pkgrel=1
pkgdesc='ffmpeg port (for Nintendo Switch homebrew development)'
arch=('any')
url='https://ffmpeg.org/'
license=('LGPL' 'GPL', 'GPL3')
options=(!strip staticlibs)
makedepends=('switch-pkg-config' 'dkp-toolchain-vars')
depends=('switch-zlib' 'switch-bzip2' 'switch-libass' 'switch-libfribidi'
         'switch-freetype' 'switch-harfbuzz' 'switch-mbedtls')
source=("https://ffmpeg.org/releases/ffmpeg-$pkgver.tar.xz" "ffmpeg.patch")
sha256sums=(
 'SKIP'
 'SKIP'
)
groups=('switch-portlibs')

prepare() {
  cd ffmpeg-$pkgver
  patch -Np1 -i "$srcdir/ffmpeg.patch"
}

build() {
  cd ffmpeg-$pkgver

  source /opt/devkitpro/switchvars.sh

  ./configure --prefix=$PORTLIBS_PREFIX --disable-shared --enable-static \
    --cross-prefix=aarch64-none-elf- \
    --enable-cross-compile \
    --arch=aarch64 \
    --target-os=horizon \
    --enable-pic \
    --extra-cflags='-D__SWITCH__ -D_GNU_SOURCE -O2 -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIC -ftls-model=local-exec' \
    --extra-cxxflags='-D__SWITCH__ -D_GNU_SOURCE -O2 -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIC -ftls-model=local-exec' \
    --extra-ldflags='-fPIE -L${PORTLIBS_PREFIX}/lib -L${DEVKITPRO}/libnx/lib' \
    --disable-runtime-cpudetect \
    --disable-programs \
    --disable-debug \
    --disable-doc \
    --disable-autodetect \
    --disable-avdevice \
    --disable-hwaccels \
    --disable-encoders \
    --enable-swscale \
    --enable-swresample \
    --enable-network \
    --disable-protocols \
    --enable-protocol='file,http,tcp,udp,rtmp,hls,https,tls,ftp' \
    --enable-asm --enable-neon \
    --enable-zlib --enable-bzlib \
    --enable-libass --enable-libfreetype \
    --enable-libfribidi --enable-mbedtls \
    --enable-version3 --enable-demuxer=hls
  
  sed 's/#define HAVE_GETNAMEINFO 1/#define HAVE_GETNAMEINFO 0/g' -i config.h

  make -j$(nproc)
}

package() {
  cd ffmpeg-$pkgver

  source /opt/devkitpro/switchvars.sh

  make DESTDIR="$pkgdir" install

  # remove examples
  rm -r "$pkgdir"${PORTLIBS_PREFIX}/share
}